/**
 * Form Layout by Karla.
 * Functional coding by Karla.
 */

package Forms;
import java.sql.Date;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.toedter.calendar.JDateChooser;

import Classes.Booking;
import Classes.CreditCard;
import Classes.Customers;
import Classes.Payments;
import Classes.Validate;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import teComponents.teJButton;
import teComponents.teJComboBox;
import teComponents.teJFrame;
import teComponents.teJinternalFrame;
import teComponents.teJLabel;
import teComponents.teJLabelWarning;
import teComponents.teJList;
import teComponents.teJPanel;
import teComponents.teJTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MngCreditCardForm  extends teComponents.teJinternalFrame {
	private teJLabel lblCCExpiry;
	private teJLabel lblCCNo;
	private JTextPane jTextPane1;
	private teJTextField txtCustId;
	private ButtonGroup buttonGroup1;
	private teJButton btnDelCC;
	private teJList lstErrors;
	private teJComboBox cboCCType;
	private teJLabel lblCCType;
	private teJTextField txtCCNo;
	private teJPanel teSelected;
	private teJComboBox cboCustomer;
	private teJLabel lblCustID;
	private JDateChooser dExpiry;
	private Vector<String> errorMsg = new Vector<String>();
	private String customerId;
	NumberFormat nf = NumberFormat.getCurrencyInstance();
	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
	CreditCard genCC = new CreditCard();
	private teJTextField txtCCId;
	private teJButton btnSave;

	/*	
	public void setCustomerId(int customer) {
		this.customerId = customer;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				BookingPmtsForm inst = new BookingPmtsForm();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
*/	
	public MngCreditCardForm(String custId) {
		super();
		initGUI(custId);
		customerId = custId;
	}
	public MngCreditCardForm(teJFrame caller, String custId) {
		super(caller);
		initGUI(custId);
		customerId = custId;
	}
	private void initGUI(String custId) {
		try {
			{
				customerId = custId;
				this.setTitle("Maintain Credit Card");
			}
			{
				this.setSize(476, 305);
			}
			{
				lblCustID = new teJLabel();
				getContentPane().add(lblCustID);
				lblCustID.setText("Customer: ");
				lblCustID.setBounds(12, 34, 73, 14);
			}
			{
				txtCustId = new teJTextField();
				getContentPane().add(txtCustId);
				txtCustId.setBounds(85, 30, 93, 18);
			}
			{
				teSelected = new teJPanel();
				getContentPane().add(teSelected);
				teSelected.setBounds(17, 65, 437, 155);
				teSelected.setLayout(null);
				{
					txtCCNo = new teJTextField();
					teSelected.add(txtCCNo);
					txtCCNo.setBounds(165, 56, 171, 18);
				}
				{
					lblCCNo = new teJLabel();
					teSelected.add(lblCCNo);
					lblCCNo.setText("Credit Card Number: ");
					lblCCNo.setBounds(6, 60, 159, 14);
				}
				{
					lblCCExpiry = new teJLabel();
					teSelected.add(lblCCExpiry);
					lblCCExpiry.setText("Expiry Date: ");
					lblCCExpiry.setBounds(31, 91, 133, 14);
				}
				{
					lblCCType = new teJLabel();
					teSelected.add(lblCCType);
					lblCCType.setBounds(18, 25, 146, 14);
					lblCCType.setText("Credit Card Type: ");
				}
				{
					dExpiry = new JDateChooser();
					teSelected.add(dExpiry);
					dExpiry.setBounds(164, 86, 172, 18);					
				}
				{
					ComboBoxModel CCTypeModel = 
						new DefaultComboBoxModel(
								new String[] { "Select a type", "AMEX", "Diners", "MC", "VISA" });
					cboCCType = new teJComboBox();
					teSelected.add(cboCCType);
					cboCCType.setModel(CCTypeModel);
					cboCCType.setBounds(164, 16, 172, 28);
				}
				{
					ListModel lstErrorsModel = 
						new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
					lstErrors = new teJList();
					lstErrors.setVisible(false);
					teSelected.add(lstErrors);
					lstErrors.setModel(lstErrorsModel);
					lstErrors.setBounds(6, 111, 418, 36);
				}
				{
					txtCCId = new teJTextField();
					teSelected.add(txtCCId);
					txtCCId.setBounds(342, 21, 10, 18);
					txtCCId.setVisible(false);
				}
			}
			{
				btnDelCC = new teJButton();
				getContentPane().add(btnDelCC);
				btnDelCC.setText("Delete");
				btnDelCC.setBounds(128, 233, 86, 25);
				btnDelCC.setToolTipText("Delete the selected Credit Card from the database.");
				btnDelCC.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						CreditCard c = new CreditCard();
					    String tmpID = txtCCId.getText();
						if (tmpID != "")
						{
							int ccId = c.nextCCId();
							c.setCcID(Integer.parseInt(txtCCId.getText()));
							c.deleteCC();
							txtCCId.setText("");
							txtCCNo.setText("");
							cboCCType.setSelectedIndex(0);
							dExpiry.setDate(new Date(System.currentTimeMillis() - 100));
							closeForm();
						}
					}
				});
			}
			{
				btnSave = new teJButton();
				getContentPane().add(btnSave);
				btnSave.setText("Save");
				btnSave.setBounds(226, 232, 100, 25);
				btnSave.setToolTipText("Save the changes to the credit card information to  the database.");
				btnSave.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						boolean valid = ValidateCC();
						if (valid)
						{
							CreditCard c = new CreditCard();
							c.setCcNum(txtCCNo.getText());
							c.setCcName((String)cboCCType.getSelectedItem());
							c.setCustID(Integer.parseInt(txtCustId.getText()));
						    
						    java.util.Date dt = dExpiry.getDate();
						    java.sql.Date sqlDate2 = new java.sql.Date(dt.getTime());
						    c.setCcDate(sqlDate2);
						    String tmpID = txtCCId.getText();
							if (tmpID != "")
							{
								int ccId = c.nextCCId();
								c.setCcID(ccId);
								c.saveCC();
							}
							else
							{
								c.setCcID(Integer.parseInt(txtCCId.getText()));
								c.updateCC();
							}
							closeForm();
						}
						else
						{
							setError();
							lstErrors.setVisible(true);
						}
					}
				});
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		txtCustId.setText(customerId);
		CreditCard cc = genCC.retrieveCC(txtCustId.getText());
		if (cc.getCcName() != null)
		{
			cboCCType.setSelectedItem(cc.getCcName());
			txtCCId.setText(Integer.toString(cc.getCcID()));
		}
		else
		{
			cboCCType.setSelectedIndex(0);
		}
		txtCCNo.setText(cc.getCcNum());
		dExpiry.setDate(cc.getCcDate());
	}
	

	private void setCCDetail(Payments pmts) {
	}
	

	private boolean ValidateCC()
	{
		errorMsg.clear();
		boolean b = true;
		String tmpCCNo = txtCCNo.getText();
		if (Validate.genericText(txtCCNo.getText()) == false)
		{
			errorMsg.addElement("Credit card number is invalid");
			b = false;
		}
		if (txtCCNo.getText().equals(""))
		{
			errorMsg.addElement("Enter a credit card number");
			b = false;
		}
		if (cboCCType.getSelectedItem() == "Select a type")
		{
			errorMsg.addElement("You must select a card type");
			b = false;
		}
		if (dExpiry.getDate() == null)
		{
			errorMsg.addElement("You must select an expiry date");
			b = false;
		}
		if (dExpiry.getDate().before(new Date(System.currentTimeMillis() - 100)))
		{
			errorMsg.addElement("Expiry date must be in the future");
			b = false;
		}
		return b;
	}	
	
	private void setError()
	{
		ListModel newLstAllModel = 
			new DefaultComboBoxModel(errorMsg);
		lstErrors.setModel(newLstAllModel);
		//**
		//change colour of text to red #c62E04, rgb 198,42,3
		lstErrors.setForeground(new java.awt.Color(198,42,3));

		//set the selection colour to the background so 
		//it doesn't look like anything's getting selected
		lstErrors.setSelectionBackground(lstErrors.getBackground());
		//set focusable to false
		lstErrors.setFocusable(false);						
	}
	
	private void closeForm()
	{
		this.setVisible(false);
	}
}
